<?php


namespace App\Models;


use Base\BaseModel;
use Lib\DB;

class UserModel extends BaseModel
{
    protected $table = 'user';

    /**
     * 使用openid获取用户
     * @param $openid
     * @param array $fields
     * @return mixed
     */
    public function getUserByOpenid($openid, $fields = "*")
    {
        return $this->baseSelectFirst($fields, [
            'openid' => $openid,
            'is_del' => 0
        ]);
    }

    /**
     * 使用uid获取用户信息
     * @param $uid
     * @param array $fields
     * @return array
     */
    public function getUserByUid($uid, $fields = '*')
    {
        return $this->baseSelectFirst($fields, [
            'id' => $uid,
            'is_del' => 0
        ]);
    }

    /**
     * 新增用户数据
     * @param $data
     * @return int
     */
    public function insertUser($data)
    {
        return $this->baseInsert($data);
    }

    /**
     * 获取用户索引数组
     * @param $uidArr
     * @param array $fields
     * @return array
     */
    public function getUserDirByUid($uidArr, $fields = '*')
    {
        return $this->baseCreateDictionary($uidArr, 'id', 'id', $fields);
    }

    public function updateUserByUid($uid, $data)
    {
        $db = DB::init();
        $res = $db->update(
            $this->table,
            $data,
            ['id' => $uid]
        );
        return $res;
    }
}
